<?php
/*
 * Template Name: Inscription
 */
?>
<?php
$error = null;
$msg = null;
if (!empty($_POST)) {
	$d = $_POST;
	if(!is_email($d['mail']) || !empty($d['prenom']) || !empty($d['login']) || !empty($d['nom']) || !empty($d['mobile']) || !empty($d['password']) || !empty($d['dtn'])){

	        $user = wp_insert_user(array(
	            'user_login' => $d['login'],
	            'user_pass'  => $d['password'],
	            'user_email' => $d['mail'],
				'user_url' => $d['ad_site'],
	            'user_registered' => date('Y-m-d H:i:s')
	        ));
	
			
			
	        if(is_wp_error($user)){
	            $error = $user->get_error_message(); 
	        }else{
				update_user_meta($user, 'first_name', $d['prenom']);
				update_user_meta($user, 'last_name', $d['nom']);
				update_user_meta($user, 'description', $d['presentation']);
				update_user_meta($user, 'statu', $d['presentation']);
				add_user_meta($user, 'tel', $d['mobile']);
				add_user_meta($user, 'date_naissance', $d['dtn']);
				add_user_meta($user, 'viadeo', $d['ad_viadeo']);
				add_user_meta($user, 'linkedin', $d['ad_linkedin']);
				add_user_meta($user, 'statut', $d['statut']);
				
	            $msg = 'Vous êtes maintenant inscrit sur le portail Miage Descartes Alumni. <br /> Vous allez recevoir un mail dans quelques instants';
	            $headers = 'From: '.get_option('admin_email')."\r\n";
	            wp_mail($d['mail'], 'Inscription réussie', $msg, $headers );
	            $d = array();
	        }
	}
	else {
		$error = 'Veuillez remplir les champs obligatoires'; 
	}
	
}

?>
 <?php get_header() ?>

<div class="middle-highlight-container">
	<div class="middle">
    	<h2 class="page-subtitle">Inscription</h2>
  	</div>
</div>


<?php //get_sidebar() ?>

<div id="content-container">
	<?php
		if (isset($error)) {
			echo '<div id="erreur">';
			echo '<div class="erreur">'.$error.'</div><br />';
			echo '</div>';
		}
		
		if (isset($msg)) {
			echo '<div id="info">';
			echo '<span>'.$msg.'</span><br />';
			echo '</div>';
		}
	?>
	<div class='txterror'>Les champs marqué d'un <span class="style_star">*</span> sont obligatoires</div>
	<div class="formulaire">
		<form name='formulaire' action="<?php echo $_SERVER['REQUEST_URI']; ?>" method='post'>
			<table>
				<th><center><h5>Formulaire d&#039;inscription</h5></center></th>
				<tr><td><label>Vous &ecirc;tes :</label></td></tr>
						<td>
							<input type="radio" class="inputRadio" name="statut" value="Etudiant"/><span class="nomRadio">Etudiant</span>
							<input type="radio" class="inputRadio" name="statut" value="Membre d&#039;honneur" /><span class="nomRadio">Membre d&#039;honneur</span>
							<input type="radio" class="inputRadio" name="statut" value="Particuliers" /><span class="nomRadio">Particuliers</span>
						</td>
				<tr><td>Nom* : </td><td><input name='nom' type='text' size='50'></td></tr>
				<tr><td>Pr&eacute;nom* : </td><td><input name='prenom' type='text' size='50'></td></tr>
				<tr><td>Pseudo* : </td><td><input name='login' type='text' size='50'></td></tr>
				<tr><td>Mot de passe* :</td><td><input name='password' type='password' size='50'></td></tr>
				<tr><td>T&eacute;l&eacute;phone* :</td><td><input name='mobile' type='text' size='50'></td></tr>
				<tr><td>Adresse &eacute;lectronique (email)* :</td><td><input name='mail' type='text' size='50'></td></tr>
				<tr><td>Date de naissance* :</td><td><input name='dtn' type='text' size='50'></td></tr>
				<tr><td>Br&egrave;ve pr&eacute;sentation :</td><td><textarea name='presentation' cols='40' rows='10'></textarea></td></tr>
				<tr><td>Adresse d&#039;un site internet :</td><td><input name='ad_site' type='text' size='50'></td></tr>
				<tr><td>Adresse du compte Viadeo :</td><td><input name='ad_viadeo' type='text' size='50'></td></tr>
				<tr><td>Adresse du compte Linkedin :</td><td><input name='ad_linkedin' type='text' size='50'></td></tr>
				<tr><td>Image du profil (ou avatar) :</td><td><input type="file" name="file" value="" />
				<center>
				<tr>
					<td>
						<input name='soumettre' type='submit' value="S'inscrire"></td></tr>
					</td>
				</center>
			</table>
		</form>
	</div>
	 <?php get_footer() ?>
</div>